package com.ll.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.ll.bean.LinkMsg;
import com.ll.bean.LocationMsg;
import com.ll.bean.Msg;
import com.ll.bean.PicMsg;
import com.ll.bean.TextMsg;

public class DB
{
	private static Context initial = null;
	private static DataSource ds = null;

	static
	{
		try
		{
			initial = new InitialContext();
			ds = (DataSource) initial.lookup("java:comp/env/jdbc/webchat");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws Exception
	{
		return ds.getConnection();
	}

	public static boolean insertMsg(Msg msg)
	{

		boolean re = false;
		Connection con = null;
		try
		{
			con = ds.getConnection();

			int falg = 0;
			PreparedStatement st = con
					.prepareStatement("INSERT INTO t_msg_income VALUES(?,?,?,?,?,?,?)");
			st.setString(1, msg.getMsgId());
			st.setString(2, msg.getToUserName());
			st.setString(3, msg.getFromUserName());
			st.setString(4, msg.getType());
			st.setString(5, msg.getCreateTime());
			st.setInt(6, 0);
			st.setTimestamp(7, new Timestamp(new java.util.Date().getTime()));
			falg = st.executeUpdate();

			if (falg == 1)
			{
				if (msg.getType().equalsIgnoreCase("text"))
				{
					re = insertTextMsg(con, (TextMsg) msg);
				}
				else if (msg.getType().equalsIgnoreCase("image"))
				{
					re = insertPicMsg(con, (PicMsg) msg);
				}
				else if (msg.getType().equalsIgnoreCase("location"))
				{
					re = insertLocationMsg(con, (LocationMsg) msg);
				}
				else if (msg.getType().equalsIgnoreCase("link"))
				{
					re = insertLinkMsg(con, (LinkMsg) msg);
				}

			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (con != null)
					con.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

		}

		return re;
	}

	private static boolean insertLinkMsg(Connection con, LinkMsg msg)
			throws Exception
	{
		int falg = 0;
		PreparedStatement link = con
				.prepareStatement("INSERT INTO t_msg_income_link VALUES(?,?,?,?)");
		link.setString(1, msg.getMsgId());
		link.setString(2, msg.getTitle());
		link.setString(3, msg.getDescription());
		link.setString(4, msg.getUrl());
		falg = link.executeUpdate();
		return falg == 1;
	}

	private static boolean insertLocationMsg(Connection con, LocationMsg msg)
			throws Exception
	{
		int falg = 0;
		PreparedStatement location = con
				.prepareStatement("INSERT INTO t_msg_income_location VALUES(?,?,?,?,?)");
		location.setString(1, msg.getMsgId());
		System.out.println("[insertLocationMsg]" + msg.getLocation_X());
		location.setString(2, msg.getLocation_X());

		location.setString(3, msg.getLocation_Y());
		location.setString(4, msg.getScale());
		location.setString(5, msg.getLabel());
		falg = location.executeUpdate();
		return falg == 1;
	}

	private static boolean insertPicMsg(Connection con, PicMsg msg)
			throws Exception
	{
		int falg = 0;

		PreparedStatement image = con
				.prepareStatement("INSERT INTO t_msg_income_image VALUES(?,?)");
		image.setString(1, msg.getMsgId());
		image.setString(2, msg.getPicUrl());
		falg = image.executeUpdate();
		return falg == 1;
	}

	public static boolean insertTextMsg(Connection con, TextMsg msg)
			throws Exception
	{

		int falg = 0;
		PreparedStatement text = con
				.prepareStatement("INSERT INTO t_msg_income_text VALUES(?,?)");
		text.setString(1, msg.getMsgId());
		text.setString(2, msg.getContent());
		falg = text.executeUpdate();
		return falg == 1;
	}

}
